﻿@page "/projectedit"
@page "/projectedit/{id:int}"
@inject Csla.Blazor.ViewModel<ProjectTracker.Library.ProjectEdit> vm
@inject NavigationManager NavigationManager

<h1>Edit Project</h1>

<p class="text-danger">@vm.ViewModelErrorText</p>

<p>
  <a href="projects">Project list</a>
</p>

@if (vm.Model == null)
{
  <p>Loading...</p>
}
else 
{ 
  <table class="table">
    <thead>
      <tr>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <LabelRow Property="vm.GetPropertyInfo(nameof(vm.Model.Id))" />
      <TextInputRow Property="vm.GetPropertyInfo(nameof(vm.Model.Name))" />
      <TextAreaRow rows="5" Property="@(vm.GetPropertyInfo(nameof(vm.Model.Description)))" />
      <DateInputRow Property="@(vm.GetPropertyInfo(nameof(vm.Model.Started)))" />
      <DateInputRow Property="@(vm.GetPropertyInfo(nameof(vm.Model.Ended)))" EmptyIsMin="false" />
    </tbody>
  </table>
  <div style="background-color:lightgray">
    @if (viewMode == SubViewModes.Details)
    {
      <a href="javascript: void(0);" @onclick="ShowDefaultView">Cancel assignment</a>
      <table class="table">
        <thead>
          <tr>
            <th>Name</th>
            <th>Role</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>@selectedResource.FirstName @selectedResource.LastName</td>
            <td>
              <select @bind="selectedResource.Role">
                @foreach (var item in ProjectTracker.Library.RoleList.GetCachedList())
                {
                  <option value="@item.Key">@item.Value</option>
                }
              </select>
            </td>
            <td><a href="javascript: void(0);" @onclick="() => AddResource()">Assign</a></td>
          </tr>
        </tbody>
      </table>
    }
    else if (viewMode == SubViewModes.Select)
    {
      @if (_resourceList == null)
      {
        <p>Loading resource list...</p>
      }
      else
      {
        <a href="javascript: void(0);" @onclick="ShowDefaultView">Cancel assignment</a>
        <table class="table">
          <thead>
            <tr>
              <th>Resource</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            @foreach (var item in _resourceList)
            {
              <tr>
                <td>@item.Name</td>
                <td><a href="javascript: void(0);" @onclick="() => AssignRole(item.Id)">Select</a></td>
              </tr>
            }
          </tbody>
        </table>
      }
    }
    else if (viewMode == SubViewModes.Default)
    {
      <a href="javascript: void(0);" @onclick="SelectResource">Add resource</a>
      <table class="table">
        <thead>
          <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Role</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          @foreach (var item in vm.Model.Resources)
          {
            <tr>
              <td>@item.FirstName</td>
              <td>@item.LastName</td>
              <td>@item.RoleName</td>
              <td><a href="javascript: void(0);" @onclick="() => EditResource(item.ResourceId)">Edit</a> | <a href="javascript: void(0);" @onclick="() => RemoveResource(item.ResourceId)">Remove</a></td>
            </tr>
          }
        </tbody>
      </table>
    }
    else
    {
      <p>Error in page - please contact support</p>
    }
  </div>
  <button @onclick="vm.SaveAsync" disabled="@(!vm.Model.IsSavable)">Save</button>
}

@code {
  [Parameter]
  public int id { get; set; }

  private SubViewModes viewMode = SubViewModes.Default;
  private ProjectTracker.Library.ProjectResourceEdit selectedResource;
  private List<ProjectTracker.Library.ResourceInfo> _resourceList;

  protected override void OnInitialized()
  {
    vm.Saved += () => NavigationManager.NavigateTo("projects");
    vm.ModelPropertyChanged += async (s, e) => await InvokeAsync(() => StateHasChanged());
  }

  protected override async Task OnParametersSetAsync()
  {
    if (id == 0)
      await vm.RefreshAsync(() => Csla.DataPortal.CreateAsync<ProjectTracker.Library.ProjectEdit>());
    else
      await vm.RefreshAsync(() => Csla.DataPortal.FetchAsync<ProjectTracker.Library.ProjectEdit>(id));
  }

  private async void SelectResource()
  {
    viewMode = SubViewModes.Select;
    _resourceList = (await ProjectTracker.Library.ResourceList.GetResourceListAsync())
                      .Where(r => !vm.Model.Resources.Contains(r.Id)).ToList();
    StateHasChanged();
  }

  private void ShowDefaultView()
  {
    if (selectedResource != null)
      selectedResource.CancelEdit();
    viewMode = SubViewModes.Default;
  }

  private async void AssignRole(int resourceId)
  {
    selectedResource = (await ProjectTracker.Library.ProjectResourceEditCreator.GetProjectResourceEditCreatorAsync(resourceId)).Result;
    selectedResource.BeginEdit();
    viewMode = SubViewModes.Details;
    StateHasChanged();
  }

  private void AddResource()
  {
    selectedResource.ApplyEdit();
    if (!vm.Model.Resources.Contains(selectedResource.ResourceId))
      vm.Model.Resources.Add(selectedResource);
    ShowDefaultView();
  }

  private void EditResource(int resourceId)
  {
    selectedResource = vm.Model.Resources
      .Where(r => r.ResourceId == resourceId).FirstOrDefault();
    if (selectedResource != null)
    {
      selectedResource.BeginEdit();
      viewMode = SubViewModes.Details;
    }
  }

  private void RemoveResource(int resourceId)
  {
    vm.Model.Resources.Remove(resourceId);
  }
}
